package vivace.view;

import vivace.model.Project;

import java.util.*;

import java.awt.*;

import javax.swing.*;

public class Perspective extends JPanel implements Observer {
	private Project model;
	private Overview overview;
	private PianoRoll pianoRoll;
	private Notation notation;
	
	public Perspective(Project model) {
		this.model = model;
		this.model.addObserver(this);
		
		this.setLayout(new CardLayout());
		
		overview = new Overview(model);
		pianoRoll = new PianoRoll(model);
		notation = new Notation(model);
		
		// add to card layout
		this.add(overview, "overview");
		this.add(pianoRoll, "pianoRoll");
		this.add(notation, "notation");

		// Set the preferred size to the same as the overview's preferred size
		this.setPreferredSize(new Dimension( overview.getPreferredSize() ));
		
	}
	
	public void update(Observable o, Object arg) {

		String perspective = model.getPerspective();
		
		// Check which type of action that was performed
    	Project.Action action = this.model.getAction();
    	
		// Then perform the desired updates depending on the action
    	switch( action ){
    		
    		case PERSPECTIVE_CHANGED:

    			// fetch perspective
    			CardLayout cl = (CardLayout)(this.getLayout());
    			cl.show(this, perspective);
    			
    		case ZOOMLEVEL_CHANGED: // Note: Don't use break in the above case

    			// Set the preferred size to the same as the current view's preferred size
    			JPanel current;
    			if(perspective.equals("overview")){
    				current = overview;
    			} else if(perspective.equals("pianoRoll")){
    				current = pianoRoll;
    			} else {
    				current = notation;
    			}
    			this.setPreferredSize(new Dimension( current.getPreferredSize() ));
    			break;
    
    	}
		
		
		repaint();
	}
}
